﻿$(document).ready(function () {
    $("input[type='button'], input[type='submit'], button").button();
    $('#contacts').find('img').each(function() {
        $(this).on('mouseenter', function() {
            var tooltip = $('#img_tooltip');
            var img = $(this);
            var offset = img.offset();

            tooltip.css({
                'top': offset.top + 40,
                'left': offset.left + 40
            });
            tooltip.width(img.data('width'));
            tooltip.height(img.data('height'));
            tooltip.find('img').width(img.data('width'));
            tooltip.find('img').height(img.data('height'));
            tooltip.find('img').attr('src', img.attr('src'));
            tooltip.show();
        });
        $(this).on('mouseleave', function() {
            $('#img_tooltip').hide();
        });
    });

    $("#searchbox").autocomplete({
        source: '/Home/GetContactsFiltered',
        select: function(event, ui) {
            window.location.href = '/Home/Contact?id=' + ui.item.id;
        }
    });

    var jcropApi;
    function attachJcrop() {
        $('#contact_image').Jcrop({
            onSelect: function (coords) {
                $('#PicX').val(coords.x);
                $('#PicY').val(coords.y);
                $('#PicWidth').val(coords.w);
                $('#PicHeight').val(coords.h);
            }
        }, function () {
            jcropApi = this;
            jcropApi.setSelect([10, 10, $('#contact_image').width() - 10, $('#contact_image').height() - 10]);
        });
    }

    attachJcrop();
    

    $("#image_form").submit(function (e) {

        var formObj = $(this);
        var formURL = formObj.attr("action");
        var formData = new FormData(this);
        $.ajax({
            url: formURL,
            type: 'POST',
            data: formData,
            mimeType: "multipart/form-data",
            contentType: false,
            cache: false,
            processData: false,
            success: function (data, textStatus, jqXHR) {
                var jsonData = JSON.parse(data);
                $('#contact_image').attr("src", jsonData.path);
                $('#ServerPath').val(jsonData.serverpath);
                if (jsonData.width > 0 && jsonData.height > 0) {
                    $('#contact_image').width(jsonData.width);
                    $('#contact_image').height(jsonData.height);
                }
                if (jcropApi != null) {
                    jcropApi.destroy();
                }
                attachJcrop();
            },
            error: function (jqXHR, textStatus, errorThrown) {
            }
        });
        e.preventDefault(); //Prevent Default action. 
        return false;
    });    

    $('#image_upload').on('change', function () {
        $("#image_form").submit(); //Submit the form
    });

    $('#image_upload').on('mouseenter', function () {
        $('#image_btn_label').addClass('image_button_highlight');
    });
    $('#image_upload').on('mouseleave', function () {
        $('#image_btn_label').removeClass('image_button_highlight');
    });

    function enableDelete() {
        if ($(".contacts_table td input:checked").length > 0) {
            $("#delete_btn").removeAttr('disabled');
        } else {
            $("#delete_btn").attr('disabled', 'disabled');
        }
    }
    $(".contacts_table td input").click(function() {
        enableDelete();
    });
    enableDelete();

    function adjustHeights(parameters) {
        var contacts = $('.contacts_container');
        //contacts.css('max-height', $(window).height() - contacts.offset().top - 70);
    }
    //$('.left_bar').height($(document).height() - $('.left_bar').offset().top - $('footer').height() - 35);
    adjustHeights();
});